package com.my.test.zookeeper;

import java.io.IOException;

import org.apache.zookeeper.*;
import org.apache.zookeeper.ZooDefs.Ids;

public class App {

	public ZooKeeper getZKClient(){
		 // 创建一个与服务器的连接
		try {
			return new ZooKeeper("192.168.182.128:" + "2181", 10000, new Watcher() { 
	            // 监控所有被触发的事件
//				@Override
	            public void process(WatchedEvent event) { 
	                System.out.println("已经触发了" + event.getType() + "事件！"); 
	            } 
	        });
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return null;
	}
	public void createNode(ZooKeeper zk,String nodePath,String nodeContext){
		// 创建一个目录节点
		 try {
			zk.create(nodePath, nodeContext.getBytes(), Ids.OPEN_ACL_UNSAFE,
			   CreateMode.PERSISTENT);
			 // 创建一个子目录节点
//			zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(),
//			   Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
			System.out.println(new String(zk.getData(nodePath,false,null)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String getNodeData(ZooKeeper zk,String nodePath){
		try {
			return new String(zk.getData(nodePath,false,null));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public void UpdateNodeData(ZooKeeper zk,String nodePath,String nodeContext) throws Exception{
		if(null!=zk.exists(nodePath, true)){
			zk.setData(nodePath,nodeContext.getBytes(),-1);
		}
	}
	public static void zookeeper() throws Exception{
		// 创建一个与服务器的连接
		 ZooKeeper zk = new ZooKeeper("192.168.182.128:" + "2181", 10000, new Watcher() { 
		            // 监控所有被触发的事件
		            public void process(WatchedEvent event) { 
		                System.out.println("已经触发了" + event.getType() + "事件！"); 
		            } 
		        }); 
		 // 创建一个目录节点
		 zk.create("/testhzb", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE,
		   CreateMode.PERSISTENT); 
		 // 创建一个子目录节点
//		 zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(),
//		   Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
		 System.out.println("获取zk node data="+new String(zk.getData("/testhzb",false,null))); 
//		 // 取出子目录节点列表
//		 System.out.println(zk.getChildren("/testRootPath",true)); 
//		 // 修改子目录节点数据
//		 zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1); 
//		 System.out.println("目录节点状态：["+zk.exists("/testRootPath",true)+"]"); 
//		 // 创建另外一个子目录节点
//		 zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), 
//		   Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
//		 System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo",true,null))); 
//		 // 删除子目录节点
//		 zk.delete("/testRootPath/testChildPathTwo",-1); 
//		 zk.delete("/testRootPath/testChildPathOne",-1); 
		 // 删除父目录节点
		 zk.delete("/testhzb",-1); 
		 // 关闭连接
		 zk.close();
	}
	public static void main(String[] args) throws Exception {
		App a = new App();
		ZooKeeper zk = a.getZKClient();
//		a.createNode(zk, "/testnode4", "haha");
		System.out.println(a.getNodeData(zk,"/testnode4"));
		a.UpdateNodeData(zk,"/testnode4","ertyhujkl;'");
		System.out.println(a.getNodeData(zk,"/testnode4"));
//		System.out.println(zk.exists("/testnode4", false));
//		zk.delete("/testnode3",-1); 
//		// 关闭连接
//		zk.close();
	}
}
